Specify the table for each field by placing the table name and a period in front of the field name (for example, Sales.[Item Code] or Sales.Quantity). (Remember, square brackets must enclose a field name that has a space in it.) You also can use the wild-card identifier (*) after the table name to indicate that you want all the fields from that table.
To specify the tables you're using, place multiple table names (separated by commas) in the FROM clause of the SELECT statement.
The relationship between the tables is specified either by a WHERE clause or by a JOIN condition. These elements are discussed later in this chapter.
The statement in Listing C.1 is used to retrieve all fields from the Sales table and the Item Description and Retail fields from the Retail Items table. These tables are related by the Item Code field. Figure C.3 shows the results of the statement.
Figure C.3
Selecting fields from multiple tables produces a combined recordset.
The listing shows an underscore character at the end of each of the first three lines. This is used to break the lines for the purpose of page-width in the book. When you enter the expressions, they need to be on a single line.
Listing C.1 SALES.TXTSelecting Fields from Multiple Tables in a SQL Statement
SELECT Sales.*, [Retail Items].[Item Description], _
[Retail Items].Retail _
FROM Sales, [Retail Items] _
WHERE Sales.[Item Code]=[Retail Items].[Item Code]
You can leave out the table name when specifying fields as long as the requested field is present only in one table in the list. However, it is very good programming practice to include the table name, both for reducing the potential for errors and for readability of your code.